﻿//===================================================================================
// $$
//
//
// Code By: Code Milker V2
// Date On: 2013//
//===================================================================================


namespace Forest.Lion.Data.SqlServer.Repositories {
    using System;

    using System.Collections.Generic;

    using System.Data;

    using System.Linq;
    using Forest.Lion.Model;

    public class DealRepository : BaseRepository, IDealRepository {
        public DealRepository(IUnitOfWork unitOfWork)
            : base(unitOfWork) {
        }
        public void Create(Deal deal) {
            this.GetDbSet<Deal>().Add(deal);
            this.UnitOfWork.SaveChanges();
        }

        public Deal GetDeal(Guid dealId) {
            return this.GetDbSet<Deal>()
                .Where(c => c.Id == dealId)
                .Single();
        }

        public void Update(Deal updatedDeal) {
            this.GetDbSet<Deal>().Attach(updatedDeal);
            this.SetEntityState(updatedDeal, updatedDeal.Id == Guid.Empty
                ? EntityState.Added
                : EntityState.Modified);
            this.UnitOfWork.SaveChanges();
        }

        public void Delete(Guid dealId) {
            Deal dealToDelete = this.GetDeal(dealId);
            this.GetDbSet<Deal>().Remove(dealToDelete);
            this.UnitOfWork.SaveChanges();
        }

        public IEnumerable<Deal> Find() {
            return this.GetDbSet<Deal>().Take(100)
                .ToList();
        }

        public IEnumerable<Deal> GetAll() {
            return this.GetDbSet<Deal>().Take(100)
                .ToList();
        }

        public IEnumerable<Deal> GetDealsForCompany(Guid companyId) {
            return this.GetDbSet<Deal>()
                .Where(d => d.CompanyId == companyId)
                .ToList();
        }
    }
}
